package RMI;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
	boolean flag;
	public void start(){
		flag=true;
		System.out.println("Before");
		try {
			//Runtime.getRuntime().exec("cmd /c start C:\\Users\\sl300\\Desktop\\start.bat");
			//Runtime.getRuntime().exec("cmd /c java -jar C:\\Users\\sl300\\Desktop\\DBProject.jar");
			// Run a java app in a separate system process
			/*Process proc = Runtime.getRuntime().exec("java -jar C:\\Users\\sl300\\Desktop\\DBProject.jar");
			// Then retreive the process output
			InputStream in = proc.getInputStream();
			InputStream err = proc.getErrorStream();*/
			ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\\Users\\sl300\\Desktop\\ClientService2.jar");
			pb.directory(new File("C:\\Users\\sl300\\Desktop"));
			Process p = pb.start();
			String s=null;
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while (flag && (s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            
            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while (flag && (s = stdError.readLine()) != null) {
                System.err.println(s);
            }
	        stdInput.close();
	        stdError.close();
            System.exit(0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*String s = null;

        try {
            
	    // run the Unix "ps -ef" command
            // using the Runtime exec method:
            Process p = Runtime.getRuntime().exec("cmd /c java -jar C:\\Users\\sl300\\Desktop\\DBProject.jar");
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            
            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            
            System.exit(0);
        }
        catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }*/
		System.out.println("After");
	}
	
	public void stop(){
		synchronized (this) {
			flag=false;
		}
		
	}
	
	public static void main(String[] args){
		new Main().start();
	}
}
